package JPetStore.web.servlet;

import JPetStore.domain.Account;
import JPetStore.domain.Cart;
import JPetStore.domain.LineItem;
import JPetStore.domain.Order;
import JPetStore.persistence.LineItemDao;
import JPetStore.persistence.impl.LineItemDaoImpl;
import JPetStore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.sql.Date;
import java.time.LocalDate;

public class ConfirmOrderJQueryServlet extends HttpServlet {
    private OrderService orderService;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try{
            // 获取表单数据
            String cardtype = req.getParameter("card_type");
            String creitcard = req.getParameter("card_number");
            String exprdate = req.getParameter("expiry_date");
            String shiptofirstname = req.getParameter("first_name");
            String shiptolastname = req.getParameter("last_name");
            String shipaddr1= req.getParameter("address1");
            String shipaddr2= req.getParameter("address2");
            String shipcity = req.getParameter("city");
            String shipstate = req.getParameter("state");
            String shipzip = req.getParameter("zip");
            String shipcountry = req.getParameter("country");
            String ship = req.getParameter("ship"); // 检查是否有勾选

            HttpSession httpSession = req.getSession();
            Account account = (Account) httpSession.getAttribute("loginAccount");
            this.orderService = new OrderService();
            int orderid = orderService.getLatestOrderId() + 1;
            Cart cart = (Cart) httpSession.getAttribute("cart");
            BigDecimal totalprice = cart.getSubTotal();
            Order order = new Order();
            // 获取当前的日期
            LocalDate localDate = LocalDate.now();
            // 将LocalDate转换为java.sql.Date
            Date orderdate = Date.valueOf(localDate);
            order.init(orderid,shipaddr1,shipaddr2,shipcity,shipstate,shipzip,shipcountry,totalprice,shiptofirstname,shiptolastname,creitcard,exprdate,cardtype,orderdate);
            httpSession.setAttribute("order",order);
            this.orderService.insertOrder(account,order);

            LineItemDao lineItemDao = new LineItemDaoImpl();
            for(int i = 0;i < cart.getCartItemList().size();i++){
                LineItem lineItem = new LineItem(i+1,cart.getCartItemList().get(i),orderid);
                lineItemDao.insertLineItem(lineItem);
            }

            // 构建 HTML 字符串，回显接收到的数据
            String html = "<html><head>\n"
                    + "</head><body>\n"
                    + "<div class=\"table-container\">\n"
                    + "    <table>\n"
                    + "        <tr><th colspan=\"2\">Order<br>"+orderdate+"</th></tr>\n"
                    + "        <tr><th colspan=\"2\">Billing Address</th></tr>\n"
                    + "        <tr><td>First name:</td><td>"+account.getFirstName()+"</td></tr>\n"
                    + "        <tr><td>Last name:</td><td>"+account.getLastName()+"</td></tr>\n"
                    + "        <tr><td>Address1:</td><td>"+account.getAddress1()+"</td></tr>\n"
                    + "        <tr><td>Address2:</td><td>"+account.getAddress2()+"</td></tr>\n"
                    + "        <tr><td>City:</td><td>"+account.getCity()+"</td></tr>\n"
                    + "        <tr><td>State:</td><td>$"+account.getState()+"</td></tr>\n"
                    + "        <tr><td>Zip:</td><td>"+account.getZip()+"</td></tr>\n"
                    + "        <tr><td>Country:</td><td>"+account.getCountry()+"</td></tr>\n"
                    + "        <tr><th colspan=\"2\">Shipping Address</th></tr>\n"
                    + "        <tr><td>First name:</td><td>"+shiptofirstname+"</td></tr>\n"
                    + "        <tr><td>Last name:</td><td>"+shiptolastname+"</td></tr>\n"
                    + "        <tr><td>Address1:</td><td>"+shipaddr1+"</td></tr>\n"
                    + "        <tr><td>Address2:</td><td>"+shipaddr2+"</td></tr>\n"
                    + "        <tr><td>City:</td><td>"+shipcity+"</td></tr>\n"
                    + "        <tr><td>State:</td><td>"+shipstate+"</td></tr>\n"
                    + "        <tr><td>Zip:</td><td>"+shipzip+"</td></tr>\n"
                    + "        <tr><td>Country:</td><td>"+shipcountry+"</td></tr>\n"
                    + "    </table>\n"
                    + "</div></body></html>";

            // 设置响应内容类型为 HTML
            resp.setContentType("text/html;charset=UTF-8");
            PrintWriter out = resp.getWriter();
            // 输出 HTML 响应
            out.println(html);
            out.flush(); // 确保内容被发送
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
